﻿@{
    ViewBag.Title = string.Format("{0}-{1}-快速添加", GetString("ParentName"), GetString("PowerName"));
    Layout = "~/Views/Shared/_ListLayout.cshtml";
}
@inherits CMSWebViewPage<object>
<div id="toolbar_buttoninfo" class="datagrid-toolbar">
    <div>
        控制器名称:@Html.StringBox("ControllerName", "", true, "请输入控制器名称", null, ValidType.Length(64))
        区域名称:@Html.StringBox("AreaName", "", false, "请输入区域名称", null, ValidType.Length(64))
        @CurrentButtonTool.Create()
        <div style="color: red;">快速添加可能会导致区域名称、控制器名称和Action名称相同的权限存在，届时请手动修改重复的权限</div>
    </div>
</div>
<table id="dg_buttoninfo" data-options="url:'/buttoninfo/listjson/',border:false,fit:true,fitColumns:true,toolbar:'#toolbar_buttoninfo',singleSelect:false,pagination:true,pageSize:20">
    <thead>
        <tr>
            <th data-options="field:'ck',checkbox:true"></th>
            <th data-options="field:'Id',width:40,align:'center'">Id</th>
            <th data-options="field:'ButtonName',width:80,align:'center'">按钮名称</th>
            <th data-options="field:'IconPreview',width:40,formatter:PreviewIcon,align:'center'">图标预览</th>
            <th data-options="field:'IsWriteControllerName',formatter:BooleanToString,width:40,align:'center'">是否写入控制器</th>
            <th data-options="field:'DefaultActionName',width:80,align:'center'">默认Action名称</th>
            <th data-options="field:'DefaultExecution',width:80,align:'center'">默认执行脚本</th>
            <th data-options="field:'SortIndex',width:40,align:'center'">排序号</th>
        </tr>
    </thead>
</table>
@section Bottom{
    <script type="text/javascript">
        var objBizDatagrid = new BizDatagrid('#dg_buttoninfo');
        function AddPowers() {
            var objRows = objBizDatagrid.source.datagrid('getSelections');
            var objIds = [];
            if (!objRows || objRows.length == 0) {
                ShowMessage("请先选择一条数据");
                return;
            }
            var $ControllerName = $('#ControllerName');
            var $AreaName = $('#AreaName');
            if (!$ControllerName.validatebox("isValid")) {
                ShowMessage("请输入控制器名称");
                return;
            }
            //if (!$AreaName.validatebox("isValid")) {
            //    ShowMessage("请输入区域名称");
            //    return;
            //}
            ConfirmMessage("确认添加吗", function (isConfirm) {
                if (!isConfirm) {
                    return;
                }
                $.each(objRows, function (i, objRow) {
                    objIds.push(objRow.Id);
                });
                var objAjaxEntity = new AjaxEntity();
                objAjaxEntity.url = AppendTransfers("/powerinfo/addpowers");
                objAjaxEntity.data = { powerId: "@GetInt("powerId")", buttonIds: objIds.join(), controllerName: $ControllerName.val(), areaName: $AreaName.val() };
                objAjaxEntity.executing = function () {
                    ShowProcess();
                    return true;
                };
                objAjaxEntity.executeSuccess = function (InvokeResult) {
                    if (InvokeResult.ResultCode == "Success") {
                        ShowMessage(InvokeResult.ResultMessage);
                        var powerId = '@GetString("powerId")';
                        var $tree = window.parent.objBizTree.source;
                        $tree.tree({
                            onLoadSuccess: function () {
                                var node = $tree.tree('find', powerId);
                                $tree.tree('expandTo', node.target);
                                $tree.tree('select', node.target);
                            }
                        });
                        Back();
                    } else {
                        ShowMessage(InvokeResult.ResultMessage);
                    }
                    CloseProcess();
                };
                objAjaxEntity.execute();
            });
        }
        function Back() {
            TransferToUrl("/powerinfo/powerinfoIndex?PowerName=@(GetString("ParentName"))");
        }
        function PreviewIcon(val, row) {
            return '<span class="l-btn-left l-btn-icon-left"><span class="l-btn-text"></span><span class="l-btn-icon ' + row.Icon + '"></span></span>';
        }
    </script>
}
